/**
 * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug
 * Copyright (c) <spug.dev@gmail.com>
 * Released under the AGPL-3.0 License.
 */
import React, { useState } from 'react';
import { observer } from 'mobx-react';
import { Modal, Form, Input, message } from 'antd';
import http from 'libs/http';
import store from './store';
import request from '../../../libs/request';
export default observer(function () {
  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false);

  function handleSubmit() {
    console.log("123123")
    setLoading(true);
    const formData = form.getFieldsValue();
    formData['id'] = store.record.id;
    form.validateFields().then(values =>{
      request.post('/api/v1/locker/roles', values)
      .then(res => {
        message.success('操作成功');
        store.formVisible = false;
        store.fetchRecords()
      }, () => setLoading(false))
    })
  }

  return (
    <Modal
      visible
      maskClosable={false}
      title={store.record.id ? '编辑角色' : '新建角色'}
      onCancel={() => store.formVisible = false}
      confirmLoading={loading}
      onOk={handleSubmit}>
      <Form form={form} initialValues={store.record} labelCol={{span: 6}} wrapperCol={{span: 14}}>
        <Form.Item required name="name" label="角色名称">
          <Input placeholder="请输入角色名称"/>
        </Form.Item>
        <Form.Item name="description" initialValue={""} label="备注信息">
          <Input.TextArea placeholder="请输入角色备注信息"/>
        </Form.Item>
      </Form>
    </Modal>
  )
})